package com.wzw.shoppingcart.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wzw.shoppingcart.entity.CartLine;
import com.wzw.shoppingcart.entity.Discount;
import com.wzw.shoppingcart.entity.Product;
import com.wzw.shoppingcart.mapper.DiscountMapper;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 *@author wzw
 * 2023/4/15
 */
@Service
@Setter(onMethod_ = {@Autowired})
public class DiscountService extends ServiceImpl<DiscountMapper, Discount> {

    public Discount getByType(String discountType) {
        return lambdaQuery().eq(Discount::getType, discountType).one();
    }

    public void setProductDiscountPrice(List<CartLine> cartLines, List<Product> productList, BigDecimal discountRate) {
        List<Integer> productIds = productList.stream()
                .map(Product::getProductId)
                .collect(Collectors.toList());

        cartLines.stream()
                .filter(c -> productIds.contains(c.getProductId()))
                .forEach(c -> c.getProduct()
                        .setDiscountPrice(c.getProduct().getPrice().multiply(discountRate)));
    }


}
